{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.12 \u4f7f\u7528\u88c5\u9970\u5668\u6269\u5145\u7c7b\u7684\u529f\u80fd\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u901a\u8fc7\u53cd\u7701\u6216\u8005\u91cd\u5199\u7c7b\u5b9a\u4e49\u7684\u67d0\u90e8\u5206\u6765\u4fee\u6539\u5b83\u7684\u884c\u4e3a\uff0c\u4f46\u662f\u4f60\u53c8\u4e0d\u5e0c\u671b\u4f7f\u7528\u7ee7\u627f\u6216\u5143\u7c7b\u7684\u65b9\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u662f\u7c7b\u88c5\u9970\u5668\u6700\u597d\u7684\u4f7f\u7528\u573a\u666f\u4e86\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u91cd\u5199\u4e86\u7279\u6b8a\u65b9\u6cd5 __getattribute__ \u7684\u7c7b\u88c5\u9970\u5668\uff0c\n\u53ef\u4ee5\u6253\u5370\u65e5\u5fd7\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def log_getattribute(cls):\n    # Get the original implementation\n    orig_getattribute = cls.__getattribute__\n\n    # Make a new definition\n    def new_getattribute(self, name):\n        print('getting:', name)\n        return orig_getattribute(self, name)\n\n    # Attach to the class and return\n    cls.__getattribute__ = new_getattribute\n    return cls\n\n# Example use\n@log_getattribute\nclass A:\n    def __init__(self,x):\n        self.x = x\n    def spam(self):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4f7f\u7528\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = A(42)\na.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a.spam()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u88c5\u9970\u5668\u901a\u5e38\u53ef\u4ee5\u4f5c\u4e3a\u5176\u4ed6\u9ad8\u7ea7\u6280\u672f\u6bd4\u5982\u6df7\u5165\u6216\u5143\u7c7b\u7684\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u7684\u66ff\u4ee3\u65b9\u6848\u3002\n\u6bd4\u5982\uff0c\u4e0a\u9762\u793a\u4f8b\u4e2d\u7684\u53e6\u5916\u4e00\u79cd\u5b9e\u73b0\u4f7f\u7528\u5230\u7ee7\u627f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class LoggedGetattribute:\n    def __getattribute__(self, name):\n        print('getting:', name)\n        return super().__getattribute__(name)\n\n# Example:\nclass A(LoggedGetattribute):\n    def __init__(self,x):\n        self.x = x\n    def spam(self):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6848\u4e5f\u884c\u5f97\u901a\uff0c\u4f46\u662f\u4e3a\u4e86\u53bb\u7406\u89e3\u5b83\uff0c\u4f60\u5c31\u5fc5\u987b\u77e5\u9053\u65b9\u6cd5\u8c03\u7528\u987a\u5e8f\u3001super() \u4ee5\u53ca\u5176\u5b838.7\u5c0f\u8282\u4ecb\u7ecd\u7684\u7ee7\u627f\u77e5\u8bc6\u3002\n\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u6765\u8bb2\uff0c\u7c7b\u88c5\u9970\u5668\u65b9\u6848\u5c31\u663e\u5f97\u66f4\u52a0\u76f4\u89c2\uff0c\u5e76\u4e14\u5b83\u4e0d\u4f1a\u5f15\u5165\u65b0\u7684\u7ee7\u627f\u4f53\u7cfb\u3002\u5b83\u7684\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u4e00\u4e9b\uff0c\n\u56e0\u4e3a\u4ed6\u5e76\u4e0d\u4f9d\u8d56 super() \u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7cfb\u60f3\u5728\u4e00\u4e2a\u7c7b\u4e0a\u9762\u4f7f\u7528\u591a\u4e2a\u7c7b\u88c5\u9970\u5668\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u6ce8\u610f\u4e0b\u987a\u5e8f\u95ee\u9898\u3002\n\u4f8b\u5982\uff0c\u4e00\u4e2a\u88c5\u9970\u5668A\u4f1a\u5c06\u5176\u88c5\u9970\u7684\u65b9\u6cd5\u5b8c\u6574\u66ff\u6362\u6210\u53e6\u4e00\u79cd\u5b9e\u73b0\uff0c\n\u800c\u53e6\u4e00\u4e2a\u88c5\u9970\u5668B\u53ea\u662f\u7b80\u5355\u7684\u5728\u5176\u88c5\u9970\u7684\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u70b9\u989d\u5916\u903b\u8f91\u3002\n\u90a3\u4e48\u8fd9\u65f6\u5019\u88c5\u9970\u5668A\u5c31\u9700\u8981\u653e\u5728\u88c5\u9970\u5668B\u7684\u524d\u9762\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u53ef\u4ee5\u56de\u987e\u4e00\u4e0b8.13\u5c0f\u8282\u53e6\u5916\u4e00\u4e2a\u5173\u4e8e\u7c7b\u88c5\u9970\u5668\u7684\u6709\u7528\u7684\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}